using System;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Xml;
using Microsoft.SharePoint.WebControls;

namespace XmlFieldType
{
    public class BaseXmlField : BaseFieldControl
    {
        private SPFieldXmlBase field;
        private TextBox textBox;
        private HtmlGenericControl textArea;
        private Button ellipsis;
        private HtmlGenericControl dialog;

        protected BaseXmlField()
        {
        }

        public BaseXmlField(SPFieldXmlBase parentField)
        {
            this.field = parentField;
            this.textBox = new TextBox();
            this.ellipsis = new Button();
            this.dialog = new HtmlGenericControl("div");
            this.textArea = new HtmlGenericControl("textarea");
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            textBox = new TextBox();

            dialog = new HtmlGenericControl("div");

            textArea = new HtmlGenericControl("textarea");
            textArea.ID = "code";

            dialog.ID = "dialog";
            dialog.Attributes.Add("Title", this.FieldName);
            dialog.Attributes.Add("Class", "XmlDialog");

            ellipsis = new Button();
            ellipsis.Text = "...";
            dialog.Controls.Add(textArea);

            this.Controls.Add(dialog);
            this.Controls.Add(textBox);

            ellipsis.OnClientClick = "Javascript:return showXmlDialog('" + dialog.ClientID + "','" + textArea.ClientID + "','" + textBox.ClientID + "');";

            this.Controls.Add(ellipsis);
        }

        public virtual string Text
        {
            get
            {
                this.EnsureChildControls();
                if (this.textBox == null)
                {
                    return null;
                }
                return this.textBox.Text;
            }
            set
            {
                this.EnsureChildControls();
                if (this.textBox != null)
                {
                    this.textBox.Text = value;
                }
            }
        }
        
        public override object Value
        {
            get
            {
                return this.Text;
            }
            set
            {
                if (base.Field != null)
                {
                    this.Text = base.Field.GetFieldValueForEdit(value);
                }
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            //Make sure we've got the scripts and the stylesheets
            ScriptLink.Register(this.Page, "XMLFieldType/Scripts/jquery-1.3.2.min.js", false);        
            ScriptLink.Register(this.Page, "XMLFieldType/Scripts/jquery-ui-1.7.2.custom.min.js", false);
            ScriptLink.Register(this.Page, "XMLFieldType/Scripts/popupDialog.js", false);
            ScriptLink.Register(this.Page, "XMLFieldType/CodeMirror-0.62/js/codemirror.js", false);

            CssRegistration.Register("/_Layouts/XMLFieldType/Css/ui-lightness/jquery-ui-1.7.2.custom.css");
            CssRegistration.Register("/_Layouts/XMLFieldType/CodeMirror-0.62/css/docs.css");

            base.OnPreRender(e);
        }
        
        public override void Validate()
        {
            if (ControlMode == SPControlMode.Display || !IsValid)
            {
                return;
            }

            base.Validate();

            if (Field.Required &&
                (Value == null || Value.ToString().Length == 0))
            {
                this.ErrorMessage = string.Format("{0} must have a value.",Field.Title);
                IsValid = false;
                return;
            }

            if (!(Value == null || Value.ToString().Length == 0))
            {

                //Attempt to load into an xml document
                XmlDocument doc = new XmlDocument();
                try
                {
                    doc.LoadXml(Value.ToString());
                    IsValid = true;
                    return;
                }
                catch (XmlException ex)
                {
                    //Inavlid Xml
                    this.ErrorMessage = string.Format("{0} contains invalid Xml. {1}", Field.Title, ex.Message);
                    IsValid = false;
                    return;
                }
            }
            else
            {
                IsValid = true;
                return;
            }
        }

    }
}

